Below is a C++ program to perform Matrix Multiplication. This program is successfully compiled, and the output is also given below.
Matrix Multiplication in C++
#include<iostream> using namespace std; int main () { int row1, clum1, row2, clum2; int i, j, k; int X[8][8], Y[8][8], Z[8][8]; cout << "Please Enter Rows and Columns Of Mateix X : "; cin >> row1 >> clum1; cout << "\nPlease Enter Rows and Columns of Matrix Y : "; cin >> row2 >> clum2; if (clum1 != row2) { cout << "\nNumber of Columns of X is not Equal to Number of Rows of Y "; cout << "\nso, It is not Possible to Multily Both matrixs "; exit(0); } cout << "\nPlease Enter Elements of X Matrix : "; for (i = 0; i < row1; i++) { for (j = 0; j < clum1; j++) { cin >> X[i][j]; } } cout << "\nEnter elements of matrix B : "; for (i = 0; i < row2; i++) { for (j = 0; j < clum2; j++) { cin >> Y[i][j]; } } for (i = 0; i < row1; i++) { for (j = 0; j < clum2; j++) { Z[i][j] = 0; for (k = 0; k < row2; k++) { Z[i][j] += X[i][k] * Y[k][j]; } } } cout << "\nMultiplication Of Matrix :\n "; for (i = 0; i < row1; i++) { for (j = 0; j < clum2; j++) { cout << Z[i][j] << " "; } cout << "\n"; } return 0; }
Output
Program Steps
- Take two matrix form users
- If the number of columns of matrix X is equal to the number of rows of matrix Y, then multiplication is possible.
- Multiply Both matrices and print the results on the screen
- Else Multiplication is not possible, exit the program
Related Posts